/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.sharding.admin.service.impl;

import com.lvyh.lightframe.sharding.admin.bean.ShardingDbGroupBean;
import com.lvyh.lightframe.sharding.admin.bean.ShardingDbTableBean;
import com.lvyh.lightframe.sharding.admin.bean.ShardingGroupBean;
import com.lvyh.lightframe.sharding.admin.common.ShardTableInfo;
import com.lvyh.lightframe.sharding.admin.service.ShardingMainDataService;
import com.lvyh.lightframe.sharding.admin.service.ShardingMgrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class ShardingMgrServiceImpl implements ShardingMgrService {

    @Autowired
    private ShardingMainDataService shardingMainDataService;

    private ShardingGroupBean getShardingGroupByRangeVal(long shardingValue) throws Exception {
        ShardingGroupBean condition = new ShardingGroupBean();
        List<ShardingGroupBean> list = shardingMainDataService.selectShardingGroup(condition);
        if (!CollectionUtils.isEmpty(list)) {
            for (ShardingGroupBean bean : list) {
                if (Long.parseLong(bean.getStartId()) <= shardingValue && Long.parseLong(bean.getEndId()) > shardingValue) {
                    return bean;
                }
            }
        }
        return null;
    }

    private List<ShardingDbGroupBean> getShardingDbGroupInfo(Integer groupId) throws Exception {
        ShardingDbGroupBean condition = new ShardingDbGroupBean();
        condition.setGroupId(groupId);
        return shardingMainDataService.selectShardingDbGroup(condition);
    }

    @Override
    public ShardTableInfo getShardInfo(String shardingValue) throws Exception {
        ShardTableInfo shardTableInfo = new ShardTableInfo();
        ShardingGroupBean shardingGroupBean = getShardingGroupByRangeVal(Long.parseLong(shardingValue));
        List<ShardingDbGroupBean> dbGroupList = getShardingDbGroupInfo(shardingGroupBean.getGroupId());
        long dbIndex = Long.parseLong(shardingValue) % 10;
        if (!CollectionUtils.isEmpty(dbGroupList)) {
            for (ShardingDbGroupBean bean : dbGroupList) {
                if (bean.getHashValue().contains(String.valueOf(dbIndex))) {
                    shardTableInfo.setDataBaseName(bean.getDbName());
                    shardTableInfo.setDbId(bean.getDbId());
                    shardTableInfo.setDsKey(bean.getDsKey());
                }
            }
        }

        ShardingDbTableBean shardingDbTableBean = getShardingTableByRangeVal(shardTableInfo.getDbId(), Long.parseLong(shardingValue));
        if (shardingDbTableBean != null) {
            shardTableInfo.setTableId(shardingDbTableBean.getTableId());
            shardTableInfo.setTableName(shardingDbTableBean.getTableName());
        }
        return shardTableInfo;
    }


    private ShardingDbTableBean getShardingTableByRangeVal(Integer dbId, long shardingValue) throws Exception {
        ShardingDbTableBean condition = new ShardingDbTableBean();
        condition.setDbId(dbId);
        List<ShardingDbTableBean> list = shardingMainDataService.selectShardingDbTable(condition);
        if (!CollectionUtils.isEmpty(list)) {
            for (ShardingDbTableBean bean : list) {
                if (Long.parseLong(bean.getStartId()) <= shardingValue && Long.parseLong(bean.getEndId()) > shardingValue) {
                    return bean;
                }
            }
        }
        return null;
    }
}
